package org.dreamwork.jasmine2.web.controls;

import org.dreamwork.jasmine2.configure.JasmineConfig;
import org.dreamwork.jasmine2.i18n.I18nUtil;
import org.dreamwork.jasmine2.web.IWebControl;
import org.dreamwork.jasmine2.web.html.GenericHtmlControl;
import org.dreamwork.util.StringUtil;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-2-2
 * Time: 14:15:11
 */
public class Literal extends GenericHtmlControl {
    private String id = "_" + (System.currentTimeMillis () ^ (long)(Math.random () * 999999999L));

    public String getId () {
        return id;
    }

    public Literal (CharSequence content) {
        setInnerText (content);
    }

    public Literal (Object content) {
        this (content == null ? "" : content.toString ());
    }

    public void render (PrintWriter writer) throws IOException {
        if (text == null || StringUtil.isEmpty (text.toString ())) return;

        String result = text.toString ();
        if (context.getApplication ().getAttribute (JasmineConfig.JASMINE_I18N_HOLDER) != null) {
            result = I18nUtil.eval (result, this);
        }
        result = I18nUtil.escapeI18n (result);
        writer.write (result);
    }

    public void addControl (IWebControl control) {
        throw new RuntimeException ("Can't add control into Literal");
    }

    @Override
    public IWebControl copy () {
        return new Literal (getInnerText ());
    }
}